package cn.com.gridinfo.sooc.core.my.course.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremy.arad.Arad;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.core.my.course.bean.SCCourse;

/**
 * 已收藏课程 适配器
 * Created by 刘晓晨 on 2015/12/10.
 */
public class SCCourseAdapter extends UltimateViewAdapter<SCCourseAdapter.ViewHolder> {
    Context context;
    private List<SCCourse> scCourseList;
    private boolean isPay;//平台是否收费  1：是 0 否

    public SCCourseAdapter(List<SCCourse> list, Context context) {
        this.context = context;
        this.scCourseList = list;
        isPay = Arad.preferences.getBoolean(LocalMark.IS_PAY.getName());
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    //加载布局
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_sc_course, parent, false);
        return new ViewHolder(view, true);
    }

    @Override
    public int getAdapterItemCount() {
        return scCourseList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {
        void OnItemClick(View view, SCCourse scCourse, int position);

    }

    public interface OnItemLongClickListener {
        void onItemLongClick(View view, SCCourse scCourse, int position);
    }

    /**
     * 点击删除时间接口
     */

    private OnItemLongClickListener onItemLongClickListener;

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    /**
     * 点击跳转课程时间接口
     */

    private OnItemClickListener nOnItemClickListener;

    public void setnOnItemClickListener(OnItemClickListener nOnItemClickListener) {
        this.nOnItemClickListener = nOnItemClickListener;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (position < getItemCount() && (customHeaderView != null ? position <= scCourseList.size() : position < scCourseList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            final SCCourse scCourse = scCourseList.get(position);
            Picasso.with(context).load(scCourse.getKcfm()).resize(context.getResources().getDimensionPixelSize(R.dimen.fengmian_width_in_mine),
                    context.getResources().getDimensionPixelSize(R.dimen.fengmian_height_in_mine)).placeholder(R.mipmap.course_placeholder)
                    .error(R.mipmap.course_placeholder).into(holder.kcfm);
            holder.kcmc.setText(scCourse.getKcmc());
            holder.kcjs.setText(scCourse.getKcjs());

            //判断平台是否收费 收费显示价钱，不收费显示学习人数
            if (isPay) {
                if (scCourse.getIs_charge().equals("1")) {
                    holder.kcrs.setText("￥" + scCourse.getPrice());
                    holder.kcrs.setTextColor(context.getResources().getColor(R.color.red_hint));
                } else {
                    holder.kcrs.setText("免费");
                    holder.kcrs.setTextColor(context.getResources().getColor(R.color.green));
                }
            } else {
                holder.kcrs.setText(scCourse.getKcrs() + "人学习");
            }
            holder.aoo.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItemLongClickListener.onItemLongClick(v, scCourseList.get(position), position);
                    return true;
                }
            });
            holder.aoo.setOnClickListener(new View.OnClickListener() {
                //                @Override
                public void onClick(View v) {
                    nOnItemClickListener.OnItemClick(v, scCourseList.get(position), position);
                }
            });
            //底部分割线的显示
            if (position == (scCourseList.size() - 1)) {
                holder.line.setVisibility(View.VISIBLE);
            } else {
                holder.line.setVisibility(View.GONE);
            }
        }
    }

    //加载更多
    public void loadmore(List<SCCourse> list) {
        scCourseList.addAll(list);
        notifyDataSetChanged();

    }

    //刷新
    public void refresh(List<SCCourse> list) {
        clear(scCourseList);
        scCourseList.addAll(list);
        notifyDataSetChanged();
    }

    //删除
    public void delete(int positon) {
        scCourseList.remove(positon);
        notifyDataSetChanged();

    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
    }

    public class ViewHolder extends UltimateRecyclerviewViewHolder {
        ImageView kcfm;//课程封面
        TextView kcmc;// 课程名称
        TextView kcjs;// 课程教师
        TextView kcrs;// 课程人数
        RelativeLayout aoo;//点击区域
        View line;//底部分割线

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                kcfm = (ImageView) itemView.findViewById(R.id.kcfm);//课程封面
                kcmc = (TextView) itemView.findViewById(R.id.kcmc);//课程名称
                kcjs = (TextView) itemView.findViewById(R.id.teacher);//教师名字
                kcrs = (TextView) itemView.findViewById(R.id.kcrs);//课程人数
                aoo = (RelativeLayout) itemView.findViewById(R.id.aoo);//点击区域（跳到课程详情）
                line = itemView.findViewById(R.id.line);
            }
        }
    }

}
